package com.home.cop.sysinfo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.home.cop.sysinfo.mapper.InstituteMapper;
import com.home.cop.sysinfo.model.Institute;
import com.home.cop.sysinfo.service.InstituteBusinessService;
import com.home.cop.sysinfo.service.InstituteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class InstituteServiceImpl implements InstituteService {

    @Autowired
    InstituteMapper instituteMapper;

    @Autowired
    private InstituteBusinessService businessService;
    @Override
    public PageInfo<Institute> list(Map<String, Object> params, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(instituteMapper.list(params));
    }

    @Transactional
    @Override
    public void insert(Institute institute) {
        instituteMapper.insertSelective(institute);
        if(StringUtils.isNotBlank(institute.getBusiTypes())){
            String[] busiTypes = institute.getBusiTypes().split(",");
            for (String busiType:busiTypes){
                businessService.insert(institute.getInstId(),busiType);
            }
        }
    }

    @Transactional
    @Override
    public void changeStatus(Integer instId, String status) {
        instituteMapper.changeStatus(instId,status);
    }

    @Transactional
    @Override
    public void delInstitute(Integer instId) {
        instituteMapper.delInstitute(instId);
    }

    @Transactional
    @Override
    public void updateInfo(Institute institute) {
        instituteMapper.updateByPrimaryKeySelective(institute);
        if (StringUtils.isNotBlank(institute.getBusiTypes())){
            businessService.deleteAll(institute.getInstId());
            String[] busiTypes = institute.getBusiTypes().split(",");
            for (String busiType:busiTypes){
                businessService.insert(institute.getInstId(),busiType);
            }
        }
    }

    @Override
    public List<Institute> listAll() {
        return instituteMapper.listAll();
    }

}
